package cn.com.dhcc.ddi.datasource.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.ddi.datamapping.service.DataMappingService;
import cn.com.dhcc.ddi.datasource.dao.TableColDao;
import cn.com.dhcc.ddi.datasource.dao.TableInfoDao;
import cn.com.dhcc.ddi.datasource.vo.TableColVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
/**
 * 
 * 表元数据信息服务类
 * @Author: fxx
 * @date: 2017-5-16
 *
 */
@Service
public class TableColService extends BaseService {
	@Autowired
	private TableColDao tableColDao;
	@Autowired
	private TableInfoDao tableInfoDao;
	@Autowired
	private DataMappingService dataMappingService;
	
	/**
	 * 
	 * @Title: getColsByTableId
	 * @Description: 根据表id获取表字段信息列表
	 * @param @param tabId
	 * @param @return
	 * @param @throws ServiceException 参数
	 * @return List<TableColVo> 返回类型
	 * @throws
	 */
	 public List<TableColVo> getColsByTableId(String tabId) throws ServiceException{
		 try {
			return tableColDao.getColsByTableId(tabId);
		} catch (DaoException e) {
			throw new ServiceException("根据表id获取表字段信息列表失败", e);
		}
	 }
	 
		/**
		 * 
		 * @Title: deleteTbColInfoById
		 * @Description: 根据id删除表字段信息
		 * @param @param id
		 * @param @throws DaoException 参数
		 * @return void 返回类型
		 * @throws
		 */
	    public void deleteTbColInfoByTbId(String tableId) throws ServiceException {
	    	try {
				tableColDao.deleteTbColInfoByTbId(tableId);
			} catch (DaoException e) {
				throw new ServiceException("根据id删除表字段信息", e);
			}
	    }
	    
	    /**
		  * 获取表增量字段
		  * @param  tableId  表id
		  * @return 增量字段，如没有返回null
		  * @throws ServiceException
		  */
		 public TableColVo getIncrementalColumn(String tableId) throws ServiceException {
			 try {
				 List<TableColVo> cols = tableColDao.getColsByTableId(tableId);
				 for (TableColVo col:cols) {
					 if (col.getIsIc() != null && col.getIsIc().equals("1")) {
						 return col;
					 }
				 }
				 return null;
			 } catch (DaoException e) {
					throw new ServiceException("根据表id获取表字段信息列表失败", e);
			 }
			 
		 }
	    
	/**
	 * 
	 * @Title: updateTableColInfo
	 * @Description: 更新或插入数据源表字段信息
	 * @param @param tbColInfoList
	 * @param @param tableId
	 * @param @throws ServiceException 参数
	 * @return void 返回类型
	 * @throws
	 */
	    public void updateOrInsertTableColInfo(List<TableInfoVo> tbInfoList, List<TableColVo> tbColInfoList) throws ServiceException {
	    	try {
	    		//获取列
	    		List<String> tbIds = new ArrayList<String>();
	    		Map<String, List<String>> map = new HashMap<String, List<String>>();
	    		String dsId = "";
	    		int dataMappingCount = 0;
	    		if (tbInfoList != null && tbInfoList.size() > 0) {
	    			dsId = tbInfoList.get(0).getDsId();
	    			dataMappingCount = dataMappingService.getCountByDsId(dsId);//查看是否与映射关联
	    			if (dataMappingCount == 0) {
	    				tbIds.addAll(tableInfoDao.getTableIdByDsId(dsId));
			    		for (int i = 0; i < tbIds.size(); i++) {
			    			List<String> colNameList = tableColDao.getColNamesByTbId(tbIds.get(i));
		    				map.put(tbIds.get(i), colNameList) ;
			    		}
	    			}
	    		}
	    		//更新或插入
		    	for (int i = 0; i < tbColInfoList.size(); i++) {
		    		TableColVo vo = tbColInfoList.get(i);
		    		if (tableColDao.getCount(vo.getDsId(), vo.getTableName(), vo.getColName()) == 1) {
		    			tableColDao.updateTableColInfo(vo);
		    		} else {
		    			tableColDao.insertTableColInfo(vo);
		    		}
		    		if (dataMappingCount == 0) {
		    			//移除表中存在的列
			    		if (map.get(vo.getTableId()) != null && map.get(vo.getTableId()).contains((vo.getColName()))) {
			    			map.get(vo.getTableId()).remove(vo.getColName());
			    		}
		    		}
		    	}
		    	if (dataMappingCount == 0) {
		    		//删除数据库表中删除的列
			    	for (String str : map.keySet()) {
			    		List<String> val = map.get(str);
			    		for (int i = 0; i< val.size(); i++ ) {
			    			tableColDao.deleteTbColInfo(str, val.get(i));
			    		}
					}
		    	}
	    	} catch (DaoException e) {
				throw new ServiceException("更新或插入数据源表字段信息失败", e);
			}
	    }
	    

}
